package com.concerneyes.mine.Task;

import java.util.HashMap;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.model.OrderSubmit;

import android.content.Context;
import android.text.TextUtils;

import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;
import util.ToastHelper;

/**
 * 订单相关接口
 *
 * @author Administrator
 */
public class OrderImpl {
    private Context context;
    private OrderListener listener;

    public OrderImpl(OrderListener listener) {
        this.listener = listener;
        this.context = CEApp.getInstance();
    }

    /**
     * 买家提交订单
     *
     * @param colorCode    商品颜色代码
     * @param refractiveId 镜片折光率Id
     * @param productNo    商品编号
     */

    public void submitOrder(String colorCode, String refractiveId, String productNo) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", PreferUtil.getUserId());
        params.put("colorCode", colorCode);
        params.put("refractiveId", refractiveId);
        params.put("productNo", productNo);

        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                if (data == null) {
                    ToastHelper.getInstance()._toast(R.string.network_error_tip);
                    if (listener != null) {
                        listener.onSubmitFail();
                    }
                    return;
                }
                OrderSubmit msg = null;
                if (data.getInt("code") == 200) {
                    String datas = data.getMap("data").toString();

					try {
						msg = JsonMap.parseJson(datas, OrderSubmit.class);
					} catch (Exception e) {
					}

                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.onSubmitFail();
                    }
                }
                if (msg!=null){
                    listener.onSubmitSuccess(msg);
                }
            }
        };
        http.setDebug(false);
        http.excute(Url.BaseUrl + "/order/add", params, "POST");
    }

    /**
     * 完善订单
     *
     * @param manId
     * @param dataId
     * @param id
     * @param addressId
     */
    public void completeOrder(String manId, String dataId, String id, String addressId, String remark) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", PreferUtil.getUserId());
        params.put("manId", manId);
        params.put("dataId", dataId);
        params.put("id", id);
        params.put("addressId", addressId);
        if (!TextUtils.isEmpty(remark)) {
            params.put("remark", remark);
        }
        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                if (data == null) {
                    if (listener != null) {
                        ToastHelper.getInstance()._toast(R.string.network_error_tip);
                    }
                    return;
                }

                if (data.getInt("code") == 200) {
                    if (listener != null) {
                        listener.oncompleteSuccess();
                    }
                    // context.openActivity("待付款", model);
                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.oncompleteFail();
                    }
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/order/complete", params, "POST");
    }

}
